﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.IO;

namespace Fcdbas.WinUI.Common
{
    public static class ClientSys
    {
        public static string text_separator_ = "^";
        public static char field_separator_ = Convert.ToChar(31);
        public static char record_separator_ = Convert.ToChar(30);
        public static char group_separator_ = Convert.ToChar(29);
        public static char file_separator_ = Convert.ToChar(28);
        public static char logicalunit_separator_ = Convert.ToChar(22);
        public static string date_format_ = "yyyy-MM-dd HH:mm:ss";

        public static string sUserId, sMachineId, sWebUrl, sContract, sDatabase, sUserName;
        public static string sLocCfgDir, sLocFuncDir, sLocDataDir;
        public static string sFtpServer, sFtpUser, sFtpPasswd, sFtpPort;
        public static int nFtpPort;
        public static void AddAttrToGroup(StringBuilder sbDest, string src)
        {
            sbDest.Append(group_separator_);
            sbDest.Append(src);
        }
        public static string[] GetAttrFromGroup(string src)
        {
            return src.Split(group_separator_);
        }

        
        /// <summary>
        /// 查找字符串, pos_ 从0开始
        /// </summary>
        /// <param name="str1_"></param>
        /// <param name="str2_"></param>
        /// <param name="pos_"></param>
        /// <returns></returns>
        public static int Instr(string str1_, string str2_, int pos_)
        {
            int n_result_;

            n_result_ = -1;

            if (pos_ >= 0)
                n_result_ = str1_.IndexOf(str2_, pos_);

            return n_result_;
        }
       
        /// <summary>
        /// 类似Oracle的 Instr函数， str1_: 源字符串， str2_: 查找字符串， pos_: 从..开始(从0起始) nth_: 第几次出现
        /// </summary>
        /// <param name="str1_"></param>
        /// <param name="str2_"></param>
        /// <param name="pos_"></param>
        /// <param name="nth_"></param>
        /// <returns></returns>
        public static int Instr(string str1_, string str2_, int pos_, int nth_)
        {
            int n_result_, from_, count_;
            string new_str_;

            count_ = 1;
            new_str_ = str1_;
            from_ = pos_;
            n_result_ = 0;

            while (count_ <= nth_)
            {
                n_result_ = Instr(new_str_, str2_, from_);
                if (n_result_ >= 0)
                {
                    count_ = count_ + 1;
                    from_ = n_result_ + 1;
                }
                else
                    break;
            }
            return n_result_;
        }
        
        /// <summary>
        ///  字符串截取， str1_: 源字符串， pos_: 开始字符串，从0开始； len_: 截取长度
        /// </summary>
        /// <param name="str1_"></param>
        /// <param name="pos_"></param>
        /// <param name="len_"></param>
        /// <returns></returns>
        public static string Substr(string str1_, int pos_, int len_)
        {
            string s_result_;

            s_result_ = str1_.Substring(pos_, len_);

            return s_result_;
        }
        // 字符替换，需要考虑数值和日期格式
        public static string Replace(string attr_, string str1_, string str2_)
        {
            string s_result_;

            s_result_ = attr_.Replace(str1_, str2_);

            return s_result_;
        }

        public static void ClearAttr(out string attr_)
        {
            attr_ = null;
        }

        public static void AddToAttr(string name_, string value_, ref string attr_)
        {
            attr_ = attr_ + name_ + field_separator_ + value_ + record_separator_;
        }

        public static void AddToAttr(string name_, double value_, ref string attr_)
        {
            AddToAttr(name_, Convert.ToString(value_), ref attr_);
        }

        public static void AddToAttr(string name_, DateTime value_, ref string attr_)
        {
            AddToAttr(name_, value_.ToString(date_format_), ref attr_);
        }
        /// <summary>
        /// 修改指定字段的值
        /// </summary>
        /// <param name="name_"></param>
        /// <param name="value_"></param>
        /// <param name="attr_"></param>
        public static void SetItemValue(string name_, string value_, ref string attr_)
        {
            int index1_;
            int index2_;

            index1_ = Instr(record_separator_ + attr_, record_separator_ + name_ + field_separator_, 0, 1);
            if (index1_ >= 0)
            {
                index2_ = Instr(record_separator_.ToString() + attr_, record_separator_.ToString(), index1_ + 1, 1);
                if (index1_ == 0)
                    attr_ = name_ + file_separator_ + value_ + record_separator_ + Substr(attr_, index2_, attr_.Length - index2_ + 1);
                else
                    attr_ = attr_.Replace(Substr(attr_, index1_ - 1, index2_ - index1_), record_separator_ + name_ + field_separator_ + value_);
            }
            else
            {
                AddToAttr(name_, value_, ref attr_);
            }
        }

        public static void SetItemValue(string name_, double value_, ref string attr_)
        {
            SetItemValue(name_, value_.ToString(), ref attr_);
        }

        public static void SetItemValue(string name_, DateTime value_, ref string attr_)
        {
            SetItemValue(name_, value_.ToString(date_format_), ref attr_);
        }
        /// <summary>
        /// 从指定的符串中获取指定字段的值
        /// </summary>
        /// <param name="name_">字段名</param>
        /// <param name="attr_">源字符串</param>
        /// <returns>返回的值</returns>
        public static string GetItemValue(string name_, string attr_)
        {
            /*
            int from_, len_, to_;

            len_ = name_.Length;
            from_ = Instr(record_separator_ + attr_, record_separator_ + name_, 0, 1);
            if (from_ >= 0)
            {
                to_ = Instr(attr_, record_separator_.ToString(), from_ + 1, 1);
                if (to_ >= 0)
                {
                    return Substr(attr_, from_ + len_ + 1, to_ - from_ - len_ - 1);
                }
            }

            return "";*/
            string[] nameAndValue = attr_.Split(record_separator_);
            foreach (string s in nameAndValue)
            {
                string[] sv = s.Split(field_separator_);
                if (sv.Length > 1 && sv[0] == name_ )
                    return sv[1];
                
            }
            return "";
        }

    }
}

